<template>
  <div>
    <van-nav-bar
      title="设置昵称"
      left-text="取消"
      right-text="完成"
      @click-left="onClickLeft"
      @click-right="onClickRight"
    />
    <div class="form">
      <van-field
        v-model.trim="value"
        rows="2"
        autosize
        type="textarea"
        maxlength="8"
        placeholder="请输入昵称"
        show-word-limit
      />
    </div>
  </div>
</template>

<script>
import { name } from '@/api/info.js'
export default {
  name: 'name',
  props: ['name'],
  data () {
    return {
      value: this.name
    }
  },
  methods: {
    onClickLeft () {
      this.$emit('setShow')
    },
    async onClickRight () {
      if (this.value === '') return this.$toast('请输入修改的昵称')
      this.$toast.loading({
        message: '加载中...',
        duration: 0, // 持续展示 toast
        forbidClick: true
      })
      try {
        await name({ name: this.value })
        this.$toast.success('修改成功')
        this.$emit('setData')
      } catch (error) {
        this.$toast.fail('修改失败')
      }
    }
  }
}
</script>

<style lang="less" scoped>
.form {
  margin: 20px;
}
</style>
